#include <stdio.h>
#include <string.h>
#include "course.h"

void EditCourse(struct Course *courses)
{
	// edit course code here
	while (1)
	{
		/*print menu*/
		printf("\n============== Edit Course ==============\n");
		printf("\t1. Add Course\n");
		printf("\t2. Delete Course\n");
		printf("\t3. Delete All Courses\n");
		printf("\t4. Modify Course\n");
		printf("\t5. Back\n");
		printf("=========================================\n\n");
		printf("Enter a number and choose an choiceation:");
		int choice;
		scanf("%d", &choice);

		/*selection*/
		if (choice == 5)
			break;
		switch (choice)
		{
		case 1:
			AddCourse(courses);
			break;
		case 2:
			DeleteCourse(courses);
			break;
		case 3:
			DeleteAllCourses(courses);
			break;
		case 4:
			ModifyCourse(courses);
			break;
		default:
			printf("\nInvalid Operation!\n");
			break;
		}
	}
	return;
}

void AddCourse(struct Course *courses)
{
	while (1)
	{
		char code[10];
		char courseName[50];

		// display and count courses
		int courseNum = displayCourses(courses);

		// input course code
		printf("Enter NEW course ID (Character+Number, lenghth < 10) or any single key back:");
		scanf("%s", code);

		// check exit operation
		if (code[1] == '\0')
		{
			return;
		}

		// check if code is duplicated
		int flag = 0;
		for (int i = 0; i < courseNum; i++)
		{
			if (strcmp(courses[i].code, code) == 0)
			{
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nDuplicated Course Code!\n\n");
			continue; // back to start
		}
		else
		{
			strcpy(courses[courseNum].code, code);
		}

		// input course name
		printf("Enter course name (less than 50 characters with spaces):");
		int c;
		while ((c = getchar()) != '\n' && c != EOF)
			; // Clear input buffer
		fgets(courseName, sizeof(courseName), stdin);

		// Remove the newline character if it's present
		size_t len = strlen(courseName);
		if (len > 0 && courseName[len - 1] == '\n')
		{
			courseName[len - 1] = '\0'; // Replace '\n' with '\0'
		}
		strcpy(courses[courseNum].name, courseName);

		// sucess msg
		printf("Add course successfully but evaluation items.\n");
		printf("\nThe newly added course is %s, %s.\n", code, courses[courseNum].name);

		// continue adding item
		char choice;
		printf("Continue to set the evaluation items for this course? (Y or y: yes; any other character: no): ");
		scanf("%c", &choice);
		if (choice == 'y' || choice == 'Y')
		{
			AddItems(&courses[courseNum]);
		}
		else
		{
			continue; // back to start
		}
	}
	return;
}

void DeleteCourse(struct Course *courses)
{
	while (1)
	{
		int no;
		// display and count courses
		int courseNum = displayCourses(courses);
		printf("Enter a number to delete the related course or any other key back:");
		scanf("%d", &no);

		// if no match then exit
		if (no <= 0 || no > MAXCOURSES || courses[no - 1].code[0] == '\0')
			return;

		// move forward
		int i = 0;
		while (1)
		{
			if (courses[no - 1 + i].code[0] == '\0')
				break;
			courses[no - 1 + i] = courses[no + i];
			i++;
		}
	}
	return;
}

void DeleteAllCourses(struct Course *courses)
{
	for (int i = 0; i < MAXCOURSES; i++)
	{
		// set code and name to empty
		strcpy(courses[i].code, "");
		strcpy(courses[i].name, "");
		// set all grade items to empty
		for (int j = 0; j < MAXGRADEITEMS; j++)
		{
			deleteAllGrade(&(courses[i].gradeitems[j]));
			strcpy(courses[i].gradeitems[j].name, "");
			courses[i].gradeitems[j].fullmark = 0;
			courses[i].gradeitems[j].percentage = 0;
		}
	}
	printf("\nAll courses are deleted!\n");
	return;
}

void ModifyCourse(struct Course *courses)
{
	while (1)
	{
		// display and count courses
		int courseNum = displayCourses(courses);
		int no;
		printf("Enter a number to modify the related course details or any other key back:");
		scanf("%d", &no);

		// if no match then exit
		if (no <= 0 || no > MAXCOURSES || courses[no - 1].code[0] == '\0')
			return;
		ModifyCourseMenu(&courses[no - 1]);
	}
	return;
}

void ModifyCourseMenu(struct Course *course)
{
	while (1)
	{
		// display course detail
		int itemNum = getGradeItemNum(*course);
		displayHeader(*course);
		for (int i = 0; i < itemNum; i++)
		{
			displayGradeItem(course->gradeitems[i], i + 1);
		}
		displayFooter();

		// display modify course menu
		printf("\n============== Modify Course ==============\n");
		printf("1. Modify Course Name\n");
		printf("2. Modify Item Name\n");
		printf("3. Modify Item Percentage\n");
		printf("4. Modify Item Fullmark\n");
		printf("5. Add Items If No Item Yet\n");
		printf("============================================\n");
		printf("\nEnter a number to choose an operation or any other key back: ");
		int op;
		scanf("%d", &op);
		switch (op)
		{
		case 1:
			ModifyCourseName(course);
			break;
		case 2:
			ModifyItemName(course);
			break;
		case 3:
			ModifyItemPercentage(course);
			break;
		case 4:
			ModifyItemFullmark(course);
			break;
		case 5:
			AddItemsWhenEmpty(course);
			break;
		default:
			return; // exit
		}
	}
	return;
}

void ModifyCourseName(struct Course *course)
{
	char courseName[50];
	printf("You want to change name of course \"%s\" as :", course->name);
	// input course name
	int c;
	while ((c = getchar()) != '\n' && c != EOF)
		; // Clear input buffer
	fgets(courseName, sizeof(courseName), stdin);

	// Remove the newline character if it's present
	size_t len = strlen(courseName);
	if (len > 0 && courseName[len - 1] == '\n')
	{
		courseName[len - 1] = '\0'; // Replace '\n' with '\0'
	}
	strcpy(course->name, courseName);
	return;
}

void ModifyItemName(struct Course *course)
{
	int no;
	printf("Enter the No. of an Item to be modified:");
	scanf("%d", &no);
	// check if valid
	if (course->gradeitems[no - 1].name[0] == '\0')
	{
		printf("\nNo such item!\n");
		return;
	}
	printf("Enter a new name:");
	scanf("%s", course->gradeitems[no - 1].name);
	return;
}

void ModifyItemPercentage(struct Course *course)
{
	int itemNum = getGradeItemNum(*course);
	printf("Enter the new percentages in order:");
	for (int i = 0; i < itemNum; i++)
	{
		scanf("%f", &course->gradeitems[i].percentage);
	}
	return;
}

void ModifyItemFullmark(struct Course *course)
{
	int no;
	printf("Enter the No. of an Item to be modified:");
	scanf("%d", &no);
	// check if valid
	if (course->gradeitems[no - 1].name[0] == '\0')
	{
		printf("\nNo such item!\n");
		return;
	}
	printf("Enter a new fullmark:");
	scanf("%f", &course->gradeitems[no - 1].fullmark);
	return;
}

void AddItemsWhenEmpty(struct Course *course)
{
	if (course->gradeitems[0].name[0] != '\0')
	{
		printf("ERROR: Can't add items as they're exist already!\n");
		return;
	}
	AddItems(course);
	return;
}

void AddItems(struct Course *course)
{
	int inputItemNum;
	// input item number
	printf("Enter number of evaluation items for the new course:");
	scanf("%d", &inputItemNum);

	// input items
	printf("Input name, percentage, full marks for each item\n");
	printf("attention: no space in names and the total percentage should be 100):");
	for (int j = 0; j < inputItemNum; j++)
	{
		scanf("%s %f %f", course->gradeitems[j].name, &course->gradeitems[j].percentage, &course->gradeitems[j].fullmark);
		// initialize all grades to -1
		for (int k = 0; k < MAXGRADES; k++)
		{
			course->gradeitems[j].grades[k] = -1;
		}
	}
	return;
}